/*
 * Powered By XY
 * Since 2020
 */

package com.xy.biz.ts.service.impl;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

import com.xy.biz.ts.domain.bo.TrainFlowConstants;
import com.xy.biz.ts.domain.converter.TrainFlowHandleConverter;
import com.xy.biz.ts.domain.req.GetTrainRequest;
import com.xy.biz.ts.domain.resp.GetTrainFlowHandleResponse;
import com.xy.biz.uc.client.UserClient;
import com.xy.lang.domain.Page;
import com.xy.tool.context.UserContextHolder;
import org.activiti.engine.RuntimeService;
import org.activiti.engine.TaskService;
import org.activiti.engine.runtime.ProcessInstance;
import org.activiti.engine.task.Task;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.xy.lang.domain.Result;
import com.xy.biz.ts.domain.bo.TrainFlowHandleBO;
import com.xy.biz.ts.domain.query.TrainFlowHandleQuery;
import com.xy.biz.ts.service.TrainFlowHandleService;
import com.xy.biz.ts.manager.TrainFlowHandleManager;
import org.springframework.util.CollectionUtils;

/**
 *
 *
 * @author zgy
 * @date 2021-6-28
 */
@Service
public class TrainFlowHandleServiceImpl implements TrainFlowHandleService {

    @Autowired
    private TrainFlowHandleManager trainFlowHandleManager;
    @Autowired
    private UserClient userClient;

    @Override
    public Result<?> saveTrainFlowHandle(TrainFlowHandleBO trainFlowHandleBO) {
        int affectedRows = trainFlowHandleManager.saveTrainFlowHandle(trainFlowHandleBO);
        if (affectedRows > 0) {
            return Result.success();
        }
        return Result.ofErrorRowsAffected();
    }

    @Override
    public Result<?> saveAllTrainFlowHandle(Collection<TrainFlowHandleBO> collection) {
        int affectedRows = trainFlowHandleManager.saveAllTrainFlowHandle(collection);
        if (affectedRows > 0) {
            return Result.success();
        }
        return Result.ofErrorRowsAffected();
    }

    @Override
    public Result<?> updateTrainFlowHandle(TrainFlowHandleBO trainFlowHandleBO) {
        int affectedRows = trainFlowHandleManager.updateTrainFlowHandle(trainFlowHandleBO);
        if (affectedRows > 0) {
            return Result.success();
        }
        return Result.ofErrorRowsAffected();
    }

    @Override
    public Result<?> deleteTrainFlowHandle(Long trainFlowHandleId) {
        int affectedRows = trainFlowHandleManager.deleteTrainFlowHandle(trainFlowHandleId);
        if (affectedRows > 0) {
            return Result.success();
        }
        return Result.ofErrorRowsAffected();
    }

    @Override
    public TrainFlowHandleBO getTrainFlowHandle(Long trainFlowHandleId) {
        return trainFlowHandleManager.getTrainFlowHandle(trainFlowHandleId);
    }

    @Override
    public List<TrainFlowHandleBO> listTrainFlowHandleById(Collection<Long> collection) {
        return trainFlowHandleManager.listTrainFlowHandleById(collection);
    }

    /**
     * 查找多个
     *
     * @param request trainId
     */
    @Override
    public Result<List<GetTrainFlowHandleResponse>> listTrainFlowNode(GetTrainRequest request) {
        List<TrainFlowHandleBO> trainFlowHandleBOS = trainFlowHandleManager.listTrainFlowHandle(TrainFlowHandleQuery.max().trainId(request.getTrainId()).build());
        List<GetTrainFlowHandleResponse> getTrainFlowHandleResponses = TrainFlowHandleConverter.toResponse(trainFlowHandleBOS);
        getTrainFlowHandleResponses.forEach(t -> t.setPhaseName(TrainFlowConstants.getPhaseName(t.getFlowType(), t.getPhase())));
        if(!CollectionUtils.isEmpty(getTrainFlowHandleResponses)){
            userClient.buildByUser(getTrainFlowHandleResponses,
                    getTrainFlowHandleResponses.stream().map(GetTrainFlowHandleResponse::getHandleUserId).collect(Collectors.toSet()),
                    GetTrainFlowHandleResponse::getHandleUserId,
                    (t, userDTO) -> t.setHandleUserName(userDTO.getUserName()));
        }
        return Result.success(getTrainFlowHandleResponses);
    }

    @Override
    public List<TrainFlowHandleBO> listTrainFlowHandle(TrainFlowHandleQuery query) {
        return trainFlowHandleManager.listTrainFlowHandle(query);
    }


    @Override
    public int countTrainFlowHandle(TrainFlowHandleQuery query) {
        return trainFlowHandleManager.countTrainFlowHandle(query);
    }

}
